package zcw.com.lib_leet_code;

import zcw.com.lib_leet_code.util.Util;

/**
 * Created by 朱城委 on 2021/11/23.<br><br>
 *
 * 中等：旋转图像
 */
public class Topic48 {

    public static void main(String[] args) {
        Topic48 instance = new Topic48();
        int[][] array;

        array = new int[][] {
                {1}
        };
        instance.rotate(array);
        Util.printArray(array);

        array = new int[][] {
                {1, 2},
                {3, 4}
        };
        instance.rotate(array);
        Util.printArray(array);

        array = new int[][] {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };
        instance.rotate(array);
        Util.printArray(array);

        array = new int[][] {
                {5, 1, 9, 11},
                {2, 4, 8, 10},
                {13, 3, 6, 7},
                {15, 14, 12, 16}
        };
        instance.rotate(array);
        Util.printArray(array);
    }

    public void rotate(int[][] matrix) {
        int len = matrix.length;

        for(int i = 0; i < len / 2; i++) {
            for(int j = 0; j < (len + 1) / 2; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[len - 1 - j][i];
                matrix[len - 1 - j][i] = matrix[len - 1 - i][len - 1 - j];
                matrix[len - 1 - i][len - 1 - j] = matrix[j][len - 1 - i];
                matrix[j][len - 1 - i] = temp;
            }
        }
    }

    public void rotate3(int[][] matrix) {
        int len = matrix.length;

        for(int i = 0; i < len / 2; i++) {
            for(int j = 0; j < len; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[len - 1 - i][j];
                matrix[len - 1 - i][j] = temp;
            }
        }


        for(int i = 0; i < len; i++) {
            for(int j = 0; j < i; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }

    public void rotate2(int[][] matrix) {
        int len = matrix.length;
        int[][] temp = new int[len][len];

        for(int i = 0; i < len; i++) {
            for(int j = 0; j < len; j++) {
                temp[i][j] = matrix[i][j];
            }
        }


        for(int i = 0; i < len; i++) {
            for(int j = 0; j < len; j++) {
                matrix[j][len - 1 - i] = temp[i][j];
            }
        }
    }
}
